<?php

namespace app\common\model;

use think\Db;
use think\Model;

class FundsRecord extends Model
{
    // 表名
    protected $name = 'funds_record';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = true;

    // 定义时间戳字段名
    protected $updateTime = false;
    
    // 追加属性
    protected $append = [
        'create_time_text',
	    'type_text',
	    'operation_type_text',
	    'account_type_text',
    ];
	
	
	const OPE_TYPE_PLUS = 1;
	const OPE_TYPE_LESS = 2;
	
	public static $_operation_type = [
		self::OPE_TYPE_PLUS => '添加',
		self::OPE_TYPE_LESS => '减少'
	];
	
	public function getOperationTypeTextAttr($value, $data) {
		$value = $value ? $value : $data['operation_type'];
		$list = self::$_operation_type;
		return isset($list[$value]) ? $list[$value] : '';
	}
	
	
	public function getOperationTypeList()
	{
		return self::$_operation_type;
	}
	
	
	const ACCOUNT_TYPE_BALANCE = 1;
	const ACCOUNT_TYPE_INTEGRAL = 2;
	
	public static $_account_type = [
		self::ACCOUNT_TYPE_BALANCE => '余额',
		self::ACCOUNT_TYPE_INTEGRAL => '积分',
	];
	
	public function getAccountTypeTextAttr($value, $data) {
		$value = $value ? $value : $data['account_type'];
		$list = self::$_account_type;
		return isset($list[$value]) ? $list[$value] : '';
	}
	
	public function getAccountTypeList()
	{
		return self::$_account_type;
	}
	
	
	const TYPE_WITHDRAW = 1;
	const TYPE_PAY_ORDER = 2;
	const TYPE_REBATE = 3;
	const TYPE_RECHARGE = 4;
	const TYPE_WITHDRAW_RETURN = 5;
	
	public static $_type = [
		self::TYPE_WITHDRAW => '提现',
		self::TYPE_PAY_ORDER => '支付订单',
		self::TYPE_REBATE => '订单返利',
		self::TYPE_RECHARGE => '充值',
		self::TYPE_WITHDRAW_RETURN => '提现退款',
	];
	
	public function getTypeTextAttr($value, $data) {
		$value = $value ? $value : $data['type'];
		$list = self::$_type;
		return isset($list[$value]) ? $list[$value] : '';
	}



    public function getCreateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['create_time']) ? $data['create_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setCreateTimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }
    
    public function user(){
		return $this->belongsTo('user', 'user_id', 'id', '', 'left')->setEagerlyType(0);
    }
	
	/**
	 * 添加流水记录
	 * @param $userId 用户ID
	 * @param $amount 金额
	 * @param $beforeAmount 变动前金额
	 * @param $accountType 帐户类型
	 * @param $operationType 操作类型
	 * @param $type 类型
	 * @param string $orderId 订单ID
	 * @param string $remark 备注
	 * @param int $relation_user_id 相关用户ID
	 * @return bool
	 */
	public function addRecord($userId, $amount, $beforeAmount, $accountType, $operationType, $type, $orderId = '', $remark="", $relation_user_id=''){
		$data = [
			'user_id' => $userId,
			'amount' => $amount,
			'before' => $beforeAmount,
			'account_type' => $accountType,
			'operation_type' => $operationType,
			'type' => $type,
			'order_id' => $orderId,
			'remark' => $remark,
			'create_time' => time(),
		];
		$res = Db::name('fundsRecord')->insert($data);
		if($res){
			return true;
		}
		return false;
	}
}
